#include "manager_impl.h"
#include<iomanip>
#define MAXTIME 20
#define Price 2
//构造函数
ManagerImpl::ManagerImpl(int id,const char* name)
{
	user = new UserDaoImpl;
	books = new BookDaoImpl;
	user->load(reader);
	books->load(book);
	strcpy(m_name,name);
	m_id = id;
}

//添加书本
bool ManagerImpl::AddBook(Book& b)
{
	books->load(book);
	book.push_back(*(&b));
	books->save(book);
	return true;
}

//借书
bool ManagerImpl::BorrowBook(int b_id,int a_id)
{
	user->load(reader);
	books->load(book);
		
	unsigned int x = 0;
	for(;x<reader.size();x++)
	{
		if(reader[x].get_id() == a_id) break;
	}
	if(x >= reader.size())
	{
		cout << "读者的ID输入错误" << endl;
		return false;
	}
	for(unsigned int i=0;i<book.size();i++)
	{
		if(b_id != book[i].GetId())
		{
			continue;
		}
		else
		{
			if(book[i].GetAppoint() !=0 && book[i].GetAppoint() != a_id) return false;
			/*if(book[i].GetBorrow() == 0)
			{
				book[i].BorrowBook();
				reader[x].books.push_back(b_id);
				books->save(book);
				user->save(reader);
				reader.clear();
				return true;
			}*/
			if(book[i].GetBorrow() == 0)
			{
				book[i].BorrowBook();
				Book* b = new Book(book[i].GetName(),book[i].GetId());
				reader[x].books.push_back(*b);
				books->save(book);
				user->save(reader);
				return true;
			}
		}
	}
	cout << "没有此书" << endl;
	return false;
}
//还书
bool ManagerImpl::ReturnBook(int b_id,int u_id)
{
	user->load(reader);
	books->load(book);
	int time = 0;

	unsigned int x =0;
	for(;x<reader.size();x++)
	{
		if(reader[x].get_id() == u_id)  break;
	}
	if(x >= reader.size())
	{
		cout << "读者的ID输入错误" << endl;
		return false;
	}
	for(unsigned int i=0;i<book.size();i++)
	{
		if(b_id == book[i].GetId() && book[i].GetBorrow())
		{
			int score = 0;
			cout << "感谢阅读，请给这本书打个分（0分~100分）：";
			cin >> score;
			time = book[i].ReturnBook(score);
			reader[x].books.erase(reader[x].books.begin()+x);
			reader[x].bookid.push_back(b_id);
			cout << "您已经借阅"<< time << "天（10天内免费，超过一天按每天2元计算）" <<endl;
			if(time > MAXTIME)
			{
				int money = (time-MAXTIME)*Price;
				if(money > reader[x].get_money())
				{
					cout << "您的余额不足,请充值!!!" << endl;
					return false;
				}
				reader[x].set_money(reader[x].get_money() - money);
			}
			user->save(reader);
			books->save(book);
			return true;
		}
	}
	book.clear();
 	cout << "书的ID输入错误" << endl;
	return false;
}
/*
//预约
bool ManagerImpl::appointBook(int b_id,int a_id)
{
	for(int i=0;i<book.size();i++)
	{
		if(book[i].getAppoint() != 0)
		{
			cout << "此书已经有人预约" << endl;
			return false;
		}
		if(book[i].getBorrow()) 
		{
			cout << "此书以被借走" << endl;
			return false;
		}
		if(b_id = book[i].getId())
		{
			book[i].appointBook(a_id);
			return true;
		}
	} 
}
*/
//显示所有书本信息
void ManagerImpl::ShowBook(void)
{
	books->load(book);
	cout << left << setw(20) << "书的ID" << left << setw(23) << "书的名字" << left << setw(23) << "是否出借" << left << setw(23) << "是否预约"<<left << setw(27) << "借阅次数"<<left << setw(23) << "评分"<< endl;
	for(unsigned int i=0; i<book.size(); i++)
	{
		book[i].AllShow();
	}
	books->save(book);
}
void ManagerImpl::save(void)
{
	user->save(reader);
	books->save(book);
}
//析构函数
ManagerImpl::~ManagerImpl()
{
	book.clear();
}

void ManagerImpl::AddReader(Reader& r)
{
	user->load(reader);
	reader.push_back(*(&r));
	user->save(reader);
	cout << "添加成功" << endl;
	sleep(2);
}

void ManagerImpl::DelReader(void)
{
	cout << "输入要删除的读者id:";
	int id = 0;
	cin >> id;
	int flag=0;
	user->load(reader);
	for(unsigned int i=0;i<reader.size();i++)
	{
		 flag = 1;
		if(reader[i].get_id() == id)
		{
			flag = 0;
			reader.erase(reader.begin()+i);
			user->save(reader);
			break;
		}
	}
	if(!flag) cout << "删除成功" << endl;
	else  cout << "删除失败" << endl;
	sleep(2);
}

int ManagerImpl::SetMoney(int cash,int u_id)
{
	user->load(reader);
	for(unsigned int i=0;i<reader.size();i++)
	{
		if(reader[i].get_id() == u_id)
		{
			reader[i].set_money(reader[i].get_money() + cash);
		//	reader[x].set_money(reader[x].get_money() - money);
			user->save(reader);
			return reader[i].get_money();
		}
	}
	return 0;
}
void ManagerImpl::ShowReader(void)
{
	user->load(reader);
	cout << left << setw(23) << "姓名" << left << setw(19) << "id" << left << setw(20) << "预留金额" << endl;
	for(unsigned int i=0;i<reader.size();i++)
	{
		reader[i].show();
		//在借书籍
		cout << "在读书籍列表：" << endl;
		for(unsigned int j=0;j<reader[i].books.size();j++)
		{
			cout << "第" << j+1 << "本书，书编号:";
			cout << reader[i].books[j].GetId() << "    ";
			cout << "书的名字:" <<reader[i].books[j].GetName() << endl;
		}
		cout << "====================================================" << endl;
		//历史书籍
		cout << "历史书籍列表：" << endl;
		for(unsigned int j=0;j<reader[i].bookid.size();j++)
		{
			cout << "第" << j+1 << "本书，书编号:";
			cout << reader[i].bookid[j] << endl;
		}
		cout << "****************************************************" << endl;
	} 
	user->save(reader);
}
